using demo.Tool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace demo
{
    public partial class LoginJY : Form
    {
        public bool IsLogin = false; 
        private string _user_id;
        private string _password;
        public LoginJY()
        {
            InitializeComponent();
        }

        public LoginJY(string user_id)
        {
            InitializeComponent();
            this.sle_yh.Text = user_id;
            this.sle_yh.ReadOnly = true;
            //MessageBox.Show("Login_JY");
        }

        private void bt_login_Click(object sender, EventArgs e)
        {
            if (_user_id == string.Empty)
            {
                MessageBox.Show("请输入用户名:");
                return;
            }
            else if (_password == string.Empty)
            {
                MessageBox.Show("请输入密码:");
                return;
            }
            else
            {
                _user_id = sle_yh.Text;
                _password = sle_mm.Text;

                string url = ConfigurationManager.AppSettings["hisUrl"].ToString() + "His/HisLogin?user_id=" + _user_id + "&password=" + _password;
                string json = HttpGetJson(url, null);
                if (json == "0")
                {
                    MessageBox.Show("用户名或密码错误,请输入正确的用户名密码再登录!");
                    sle_yh.Focus();
                    sle_yh.SelectAll();
                    sle_mm.Text = string.Empty;
                }
                else {
                    if (json.Contains("|"))
                    {
                        IsLogin = true;
                        this.Close();
                    }
                    else {
                        MessageBox.Show("用户名或密码错误,请输入正确的用户名密码再登录!");
                        sle_yh.Focus();
                        sle_yh.SelectAll();
                        sle_mm.Text = string.Empty;
                    }
                }
            }
        }

         /// <summary>
        /// 
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="headerDic"></param>
        /// <param name="Method"></param>
        /// <returns></returns>
        private static string HttpGetJson(string Url, Dictionary<string, string> headerDic = null, string Method = "GET")
        {
            string jsons;
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);    //创建一个请求示例 
                if (headerDic != null && headerDic.Count > 0)
                {
                    foreach (var item in headerDic)
                    {
                        request.Headers[item.Key] = item.Value; //添加头
                    }
                }
                //request.Headers["键"] = "值"; //添加头
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();  //获取响应，即发送请求

                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                jsons = streamReader.ReadToEnd();
            }
            catch (Exception)
            {
                return "";
            }
            return jsons;
        }

        private void sle_yh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(13))
            {
                sle_mm.Focus();
                //_user_id = this.Text;
            }
        }

        private void sle_mm_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == Convert.ToChar(13))
            {
                bt_login.Focus();
                //_password = this.Text;
            }
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            string msg = "";
            int code = CodeHelper.getEffectiveDate(ref msg);
            if (code >= 0)
            {
                if (code < 10)
                {
                    MessageBox.Show("授权剩余天数" + code + "天，请尽快联系供应商进行授权使用。");
                }
            }
            else if (code == -99)
            {
                MessageBox.Show(msg);
                this.Close();
            }
            else
            {
                MessageBox.Show("授权已到期，请联系供应商进行授权使用。");
                this.Close();
            }
        }


        


    }
}
